package com.tomecode.tools.maven.settings.gui.frm;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;
import com.tomecode.tools.maven.settings.gui.utils.SWTResourceManager;
import com.tomecode.tools.maven.settings.model.Activation;
import com.tomecode.tools.maven.settings.model.PluginRepository;
import com.tomecode.tools.maven.settings.model.Profile;
import com.tomecode.tools.maven.settings.model.Repository;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;

/**
 * Dialog for editing profile
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmProfile extends Dialog {

	private Profile newProfile;
	private Shell shlProfile;
	private Text textProfileID;
	private Text textJDK;
	private Text textOSName;
	private Text textOSFamily;
	private Text textOSArch;
	private Text textOSVersion;
	private Text textPropertyName;
	private Text textPropertyValue;
	private Text textFileMissing;
	private Text textFileExists;
	private Table tableProperties;
	private Table tableRepositories;
	private Table tablePluginRepositories;
	private Profile existsProfile;
	private List<Profile> listOfProfiles;
	private Label labelWarning;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmProfile(Shell parent, List<Profile> listOfProfiles) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("Profile");
		this.listOfProfiles = listOfProfiles;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final Profile open(Profile existsProfile) {
		createContents(existsProfile);
		shlProfile.open();
		shlProfile.layout();
		Display display = getParent().getDisplay();
		while (!shlProfile.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return newProfile;
	}

	/**
	 * Create contents of the dialog.
	 */
	private final void createContents(Profile existsProfile) {
		this.existsProfile = existsProfile;
		shlProfile = new Shell(getParent(), getStyle());
		shlProfile.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlProfile.setSize(570, 613);

		Composite composite = new Composite(shlProfile, SWT.NONE);

		final Button btnSave = new Button(composite, SWT.NONE);
		final Profile resultProfile;
		if (existsProfile == null) {
			shlProfile.setText("New Profile");
			resultProfile = new Profile();
			btnSave.setEnabled(false);
		} else {
			shlProfile.setText("Edit Profile");
			resultProfile = new Profile(existsProfile);
			btnSave.setEnabled(true);
		}

		Button btnCancel = new Button(composite, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				shlProfile.close();
			}
		});

		btnCancel.setText("Cancel");

		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				newProfile = resultProfile;
				shlProfile.close();
			}
		});

		btnSave.setText("Save");

		Group group = new Group(composite, SWT.NONE);

		Label lblProfileId = new Label(group, SWT.NONE);
		lblProfileId.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));

		lblProfileId.setText("Profile ID");

		textProfileID = new Text(group, SWT.BORDER);
		textProfileID.setText(GuiUtils.formateNull(resultProfile.getId()));
		textProfileID.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.setId(textProfileID.getText());
				if (textProfileID.getText().trim().length() == 0) {
					labelWarning.setText("Profile ID can't be empty");
					btnSave.setEnabled(false);
				} else {
					if (existProfileId(resultProfile)) {
						btnSave.setEnabled(false);
						labelWarning.setText("Profile ID already exists");
					} else {
						labelWarning.setText("");
						btnSave.setEnabled(true);
					}

				}
			}
		});

		TabFolder tabFolderProfileItems = new TabFolder(group, SWT.NONE);

		TabItem tbtmActivation = new TabItem(tabFolderProfileItems, SWT.NONE);
		tbtmActivation.setText("Activation");

		Composite compositeActivation = new Composite(tabFolderProfileItems, SWT.NONE);
		tbtmActivation.setControl(compositeActivation);

		final Button btnActiveByDefault = new Button(compositeActivation, SWT.CHECK);
		Activation activation = resultProfile.getActivation();

		btnActiveByDefault.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		btnActiveByDefault.setSelection(activation.isActiveByDefault());
		btnActiveByDefault.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getActivation().setActiveByDefault(btnActiveByDefault.getSelection());
			}
		});
		btnActiveByDefault.setBounds(10, 10, 485, 14);
		btnActiveByDefault.setText("Active By Default");

		Label label = new Label(compositeActivation, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(10, 30, 514, 2);

		Label lblActivationJdk = new Label(compositeActivation, SWT.NONE);
		lblActivationJdk.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblActivationJdk.setBounds(10, 41, 59, 14);
		lblActivationJdk.setText("JDK");

		textJDK = new Text(compositeActivation, SWT.BORDER);
		textJDK.setText(GuiUtils.formateNull(resultProfile.getActivation().getJdk()));
		textJDK.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().setJdk(textJDK.getText());
			}
		});
		textJDK.setBounds(75, 38, 420, 19);

		Group grpOs = new Group(compositeActivation, SWT.NONE);
		grpOs.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		grpOs.setText("OS");
		grpOs.setBounds(10, 67, 522, 139);

		Label lblOsName = new Label(grpOs, SWT.NONE);
		lblOsName.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOsName.setBounds(10, 23, 59, 14);
		lblOsName.setText("Name");

		textOSName = new Text(grpOs, SWT.BORDER);
		textOSName.setText(GuiUtils.formateNull(resultProfile.getActivation().getOs().getName()));
		textOSName.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getOs().setName(textOSName.getText());
			}
		});
		textOSName.setBounds(75, 20, 397, 19);

		Label lblOSFamily = new Label(grpOs, SWT.NONE);
		lblOSFamily.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOSFamily.setBounds(10, 48, 59, 14);
		lblOSFamily.setText("Family");

		textOSFamily = new Text(grpOs, SWT.BORDER);
		textOSFamily.setText(GuiUtils.formateNull(resultProfile.getActivation().getOs().getFamily()));
		textOSFamily.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getOs().setFamily(textOSFamily.getText());
			}
		});
		textOSFamily.setBounds(75, 45, 397, 19);

		Label lblOsArch = new Label(grpOs, SWT.NONE);
		lblOsArch.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOsArch.setBounds(10, 73, 59, 14);
		lblOsArch.setText("Arch");

		textOSArch = new Text(grpOs, SWT.BORDER);
		textOSArch.setText(GuiUtils.formateNull(resultProfile.getActivation().getOs().getArch()));
		textOSArch.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getOs().setArch(textOSArch.getText());
			}
		});
		textOSArch.setBounds(75, 70, 397, 19);

		Label lblOsVersion = new Label(grpOs, SWT.NONE);
		lblOsVersion.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOsVersion.setBounds(10, 98, 59, 14);
		lblOsVersion.setText("Version");

		textOSVersion = new Text(grpOs, SWT.BORDER);
		textOSVersion.setText(GuiUtils.formateNull(resultProfile.getActivation().getOs().getVersion()));
		textOSVersion.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getOs().setVersion(textOSVersion.getText());
			}
		});
		textOSVersion.setBounds(75, 95, 397, 19);

		Label labelHelpOSName = new Label(grpOs, SWT.NONE);
		labelHelpOSName.setToolTipText("The name of the OS to be used to activate a profile.");
		labelHelpOSName.setImage(ImageFactory.HELP);
		labelHelpOSName.setBounds(488, 20, 16, 17);

		Label labelHelpOSFamily = new Label(grpOs, SWT.NONE);
		labelHelpOSFamily.setToolTipText("The general family of the OS to be used to activate a profile (e.g. 'windows')");
		labelHelpOSFamily.setImage(ImageFactory.HELP);
		labelHelpOSFamily.setBounds(488, 48, 16, 17);

		Label labelHelpOSArch = new Label(grpOs, SWT.NONE);
		labelHelpOSArch.setToolTipText("The architecture of the OS to be used to activate a profile.");
		labelHelpOSArch.setImage(ImageFactory.HELP);
		labelHelpOSArch.setBounds(488, 73, 16, 17);

		Label labelHelpOSVersion = new Label(grpOs, SWT.NONE);
		labelHelpOSVersion.setToolTipText("The version of the OS to be used to activate a profile.");
		labelHelpOSVersion.setImage(ImageFactory.HELP);
		labelHelpOSVersion.setBounds(488, 98, 16, 17);

		Label label_4 = new Label(grpOs, SWT.NONE);
		label_4.setToolTipText("This is an activator which will detect an operating system's attributes in order to activate its profile.");
		label_4.setImage(ImageFactory.HELP);
		label_4.setBounds(488, 0, 16, 17);

		Group grpProperty = new Group(compositeActivation, SWT.NONE);
		grpProperty.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		grpProperty.setText("Property");
		grpProperty.setBounds(10, 212, 522, 93);

		Label lblPropertyName = new Label(grpProperty, SWT.NONE);
		lblPropertyName.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblPropertyName.setBounds(10, 26, 59, 14);
		lblPropertyName.setText("Name");

		textPropertyName = new Text(grpProperty, SWT.BORDER);
		textPropertyName.setText(GuiUtils.formateNull(resultProfile.getActivation().getProperty().getName()));
		textPropertyName.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getProperty().setName(textPropertyName.getText());
			}
		});
		textPropertyName.setBounds(75, 23, 397, 19);

		Label lblPropertyValue = new Label(grpProperty, SWT.NONE);
		lblPropertyValue.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblPropertyValue.setBounds(10, 51, 59, 14);
		lblPropertyValue.setText("Value");

		textPropertyValue = new Text(grpProperty, SWT.BORDER);
		textPropertyValue.setText(GuiUtils.formateNull(resultProfile.getActivation().getProperty().getValue()));
		textPropertyValue.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getProperty().setValue(textPropertyValue.getText());
			}
		});
		textPropertyValue.setBounds(75, 48, 397, 19);

		Label labelHelpPropertyName = new Label(grpProperty, SWT.NONE);
		labelHelpPropertyName.setToolTipText("The name of the property to be used to activate a profile.The name of the property to be used to activate a profile");
		labelHelpPropertyName.setImage(ImageFactory.HELP);
		labelHelpPropertyName.setBounds(488, 23, 16, 17);

		Label labelHelpProeprtyValue = new Label(grpProperty, SWT.NONE);
		labelHelpProeprtyValue.setToolTipText("The value of the property to be used to activate a profile.");
		labelHelpProeprtyValue.setImage(ImageFactory.HELP);
		labelHelpProeprtyValue.setBounds(488, 49, 16, 17);

		Label label_3 = new Label(grpProperty, SWT.NONE);
		label_3.setToolTipText("This is the property specification used to activate a profile. If the value field is empty, then the existence of the named property will activate the profile, otherwise it does a case-sensitive match against the property value as well.");
		label_3.setImage(ImageFactory.HELP);
		label_3.setBounds(488, 0, 16, 17);

		Group grpFile = new Group(compositeActivation, SWT.NONE);
		grpFile.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		grpFile.setText("File");
		grpFile.setBounds(10, 312, 522, 93);

		Label lblFileMissing = new Label(grpFile, SWT.NONE);
		lblFileMissing.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblFileMissing.setBounds(10, 26, 59, 14);
		lblFileMissing.setText("Missing");

		textFileMissing = new Text(grpFile, SWT.BORDER);
		textFileMissing.setText(GuiUtils.formateNull(resultProfile.getActivation().getFile().getMissing()));
		textFileMissing.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getFile().setMissing(textFileMissing.getText());
			}
		});
		textFileMissing.setBounds(75, 23, 397, 19);

		Label lblFileExists = new Label(grpFile, SWT.NONE);
		lblFileExists.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblFileExists.setBounds(10, 51, 59, 14);
		lblFileExists.setText("Exists");

		textFileExists = new Text(grpFile, SWT.BORDER);
		textFileExists.setText(GuiUtils.formateNull(resultProfile.getActivation().getFile().getExists()));
		textFileExists.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				resultProfile.getActivation().getFile().setExists(textFileExists.getText());
			}
		});
		textFileExists.setBounds(75, 48, 397, 19);

		Label labelHelpFileMissing = new Label(grpFile, SWT.NONE);
		labelHelpFileMissing.setToolTipText("The name of the file that should be missing to activate a profile.");
		labelHelpFileMissing.setImage(ImageFactory.HELP);
		labelHelpFileMissing.setBounds(488, 26, 16, 17);

		Label labelHelpFileExists = new Label(grpFile, SWT.NONE);
		labelHelpFileExists.setToolTipText("The name of the file that should exist to activate a profile.");
		labelHelpFileExists.setImage(ImageFactory.HELP);
		labelHelpFileExists.setBounds(488, 51, 16, 17);

		Label labelHelpFile = new Label(grpFile, SWT.NONE);
		labelHelpFile
				.setToolTipText("This is the file specification used to activate a profile. The missing value will be a the location of a file that needs to exist, and if it doesn't the profile must run. On the other hand exists will test for the existence of the file and if it is there will run the profile.");
		labelHelpFile.setImage(ImageFactory.HELP);
		labelHelpFile.setBounds(488, 0, 16, 17);

		Label labelHelpActiveByDefault = new Label(compositeActivation, SWT.NONE);
		labelHelpActiveByDefault.setToolTipText("Flag specifying whether this profile is active as a default. ");
		labelHelpActiveByDefault.setImage(ImageFactory.HELP);
		labelHelpActiveByDefault.setBounds(508, 10, 16, 14);

		Label labelHelpJDK = new Label(compositeActivation, SWT.NONE);
		labelHelpJDK.setToolTipText("Specifies that this profile will be activated when a matching JDK is detected.");
		labelHelpJDK.setImage(ImageFactory.HELP);
		labelHelpJDK.setBounds(508, 38, 16, 17);

		TabItem tbtmProperties = new TabItem(tabFolderProfileItems, SWT.NONE);
		tbtmProperties.setText("Properties");

		Composite compositeProperties = new Composite(tabFolderProfileItems, SWT.NONE);
		tbtmProperties.setControl(compositeProperties);

		Button btnPropertiesDeleteAll = new Button(compositeProperties, SWT.NONE);
		btnPropertiesDeleteAll.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getProperties().clear();
				GuiUtils.fillTablePropertiesItem(tableProperties, resultProfile.getProperties());
			}
		});
		btnPropertiesDeleteAll.setText("Delete All");

		final Button btnPropertiesDelete = new Button(compositeProperties, SWT.NONE);
		btnPropertiesDelete.setEnabled(false);
		btnPropertiesDelete.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getProperties().remove(tableProperties.getItem(tableProperties.getSelectionIndex()).getText());
				GuiUtils.fillTablePropertiesItem(tableProperties, resultProfile.getProperties());
			}
		});
		btnPropertiesDelete.setText("Delete");

		final Button btnPropertiesEdit = new Button(compositeProperties, SWT.NONE);
		btnPropertiesEdit.setEnabled(false);
		btnPropertiesEdit.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				TableItem tableItem = tableProperties.getItem(tableProperties.getSelectionIndex());
				openFrmPropertyEditor(resultProfile, tableItem.getText(0), tableItem.getText(1));
			}
		});
		btnPropertiesEdit.setText("Edit");

		Button btnPropertiesAdd = new Button(compositeProperties, SWT.NONE);
		btnPropertiesAdd.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				FrmProperty frmProperty = new FrmProperty(shlProfile);
				String[] properties = frmProperty.open(null, null);
				if (properties != null) {
					resultProfile.getProperties().put(properties[0], properties[1]);
					GuiUtils.fillTablePropertiesItem(tableProperties, resultProfile.getProperties());
				}
			}
		});
		btnPropertiesAdd.setText("Add");

		tableProperties = new Table(compositeProperties, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		GuiUtils.fillTablePropertiesItem(tableProperties, resultProfile.getProperties());
		tableProperties.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tableProperties.getSelectionIndex();
				if (index != -1) {
					TableItem tableItem = tableProperties.getItem(index);
					openFrmPropertyEditor(resultProfile, tableItem.getText(0), tableItem.getText(1));
				}
			}
		});
		tableProperties.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				int index = tableProperties.getSelectionIndex();
				btnPropertiesDelete.setEnabled(index != -1);
				btnPropertiesEdit.setEnabled(index != -1);
			}
		});
		tableProperties.setHeaderVisible(true);
		tableProperties.setLinesVisible(true);

		TableColumn tblclmnName = new TableColumn(tableProperties, SWT.CENTER);
		tblclmnName.setMoveable(true);
		tblclmnName.setWidth(251);
		tblclmnName.setText("Name");

		TableColumn tblclmnValues = new TableColumn(tableProperties, SWT.CENTER);
		tblclmnValues.setMoveable(true);
		tblclmnValues.setWidth(251);
		tblclmnValues.setText("Value");
		GroupLayout gl_compositeProperties = new GroupLayout(compositeProperties);
		gl_compositeProperties.setHorizontalGroup(gl_compositeProperties
				.createParallelGroup(GroupLayout.LEADING)
				.add(gl_compositeProperties.createSequentialGroup().add(148).add(btnPropertiesAdd, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnPropertiesEdit, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6)
						.add(btnPropertiesDelete, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnPropertiesDeleteAll, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))
				.add(gl_compositeProperties.createSequentialGroup().add(1).add(tableProperties, GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)));
		gl_compositeProperties.setVerticalGroup(gl_compositeProperties.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositeProperties.createSequentialGroup().add(tableProperties, GroupLayout.PREFERRED_SIZE, 335, GroupLayout.PREFERRED_SIZE).add(29)
						.add(gl_compositeProperties.createParallelGroup(GroupLayout.LEADING).add(btnPropertiesAdd).add(btnPropertiesEdit).add(btnPropertiesDelete).add(btnPropertiesDeleteAll))));
		compositeProperties.setLayout(gl_compositeProperties);

		TabItem tbtmRepositories = new TabItem(tabFolderProfileItems, SWT.NONE);
		tbtmRepositories.setText("Repositories");

		Composite compositeRepositories = new Composite(tabFolderProfileItems, SWT.NONE);
		tbtmRepositories.setControl(compositeRepositories);

		tableRepositories = new Table(compositeRepositories, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		GuiUtils.fillTableRepositoriesItem(tableRepositories, resultProfile.getRepositories());
		final Button btnRepositoriesEdit = new Button(compositeRepositories, SWT.NONE);
		final Button btnRepositoriesDelete = new Button(compositeRepositories, SWT.NONE);

		tableRepositories.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tableRepositories.getSelectionIndex();
				if (index != -1) {
					openFrmRepositoryEdit(index, resultProfile);
				}
			}
		});
		tableRepositories.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				int index = tableRepositories.getSelectionIndex();
				btnRepositoriesEdit.setEnabled(index != -1);
				btnRepositoriesDelete.setEnabled(index != -1);
			}
		});
		tableRepositories.setHeaderVisible(true);
		tableRepositories.setLinesVisible(true);

		TableColumn tblclmnRepositoriesId = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesId.setMoveable(true);
		tblclmnRepositoriesId.setWidth(100);
		tblclmnRepositoriesId.setText("ID");

		TableColumn tblclmnRepositoriesName = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesName.setMoveable(true);
		tblclmnRepositoriesName.setWidth(100);
		tblclmnRepositoriesName.setText("Name");

		TableColumn tblclmnRepositoriesUrl = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesUrl.setMoveable(true);
		tblclmnRepositoriesUrl.setWidth(196);
		tblclmnRepositoriesUrl.setText("URL");

		TableColumn tblclmnRepositoriesLayout = new TableColumn(tableRepositories, SWT.NONE);
		tblclmnRepositoriesLayout.setWidth(100);
		tblclmnRepositoriesLayout.setText("Layout");

		Button btnRepositoriesDeleteAll = new Button(compositeRepositories, SWT.NONE);
		btnRepositoriesDeleteAll.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getRepositories().clear();
				GuiUtils.fillTableRepositoriesItem(tableRepositories, resultProfile.getRepositories());
			}
		});
		btnRepositoriesDeleteAll.setText("Delete All");

		btnRepositoriesDelete.setEnabled(false);
		btnRepositoriesDelete.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getRepositories().remove(tableRepositories.getSelectionIndex());
				GuiUtils.fillTableRepositoriesItem(tableRepositories, resultProfile.getRepositories());
			}
		});
		btnRepositoriesDelete.setText("Delete");

		btnRepositoriesEdit.setEnabled(false);
		btnRepositoriesEdit.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				openFrmRepositoryEdit(tableRepositories.getSelectionIndex(), resultProfile);
			}
		});
		btnRepositoriesEdit.setText("Edit");

		Button btnRepositoriesAdd = new Button(compositeRepositories, SWT.NONE);
		btnRepositoriesAdd.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				FrmRepository frmRepository = new FrmRepository(shlProfile, resultProfile.getRepositories());
				Repository repository = frmRepository.open(null);
				if (repository != null) {
					resultProfile.addRepository(repository);
					GuiUtils.fillTableRepositoriesItem(tableRepositories, resultProfile.getRepositories());
				}
			}
		});
		btnRepositoriesAdd.setText("Add");
		GroupLayout gl_compositeRepositories = new GroupLayout(compositeRepositories);
		gl_compositeRepositories.setHorizontalGroup(gl_compositeRepositories
				.createParallelGroup(GroupLayout.LEADING)
				.add(tableRepositories, GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
				.add(GroupLayout.TRAILING,
						gl_compositeRepositories.createSequentialGroup().addContainerGap(148, Short.MAX_VALUE).add(btnRepositoriesAdd, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED)
								.add(btnRepositoriesEdit, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnRepositoriesDelete, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED)
								.add(btnRepositoriesDeleteAll, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addContainerGap()));
		gl_compositeRepositories.setVerticalGroup(gl_compositeRepositories.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositeRepositories.createSequentialGroup().add(tableRepositories, GroupLayout.PREFERRED_SIZE, 335, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED, 51, Short.MAX_VALUE)
						.add(gl_compositeRepositories.createParallelGroup(GroupLayout.BASELINE).add(btnRepositoriesDelete).add(btnRepositoriesEdit).add(btnRepositoriesAdd).add(btnRepositoriesDeleteAll)).addContainerGap()));
		compositeRepositories.setLayout(gl_compositeRepositories);

		TabItem tbtmPluginRepositories = new TabItem(tabFolderProfileItems, SWT.NONE);
		tbtmPluginRepositories.setText("Plugin Repositories");

		Composite compositePluginRepositories = new Composite(tabFolderProfileItems, SWT.NONE);
		tbtmPluginRepositories.setControl(compositePluginRepositories);

		tablePluginRepositories = new Table(compositePluginRepositories, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		GuiUtils.fillTablePluginRepositoriesItem(tablePluginRepositories, resultProfile.getPluginRepositories());
		final Button btnPluginRepositoriesEdit = new Button(compositePluginRepositories, SWT.NONE);
		final Button btnPluginRepositoriesDelete = new Button(compositePluginRepositories, SWT.NONE);

		tablePluginRepositories.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tablePluginRepositories.getSelectionIndex();
				if (index == -1) {
					btnPluginRepositoriesEdit.setEnabled(false);
					btnPluginRepositoriesDelete.setEnabled(false);
				} else {
					openFrmPluginRepositoryEdit(index, resultProfile);
					btnPluginRepositoriesEdit.setEnabled(true);
					btnPluginRepositoriesDelete.setEnabled(true);
				}
			}
		});
		tablePluginRepositories.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				btnPluginRepositoriesEdit.setEnabled(tablePluginRepositories.getSelectionIndex() != -1);
				btnPluginRepositoriesDelete.setEnabled(tablePluginRepositories.getSelectionIndex() != -1);
			}
		});
		tablePluginRepositories.setHeaderVisible(true);
		tablePluginRepositories.setLinesVisible(true);

		TableColumn tblclmnPluginRepositoriesId = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesId.setMoveable(true);
		tblclmnPluginRepositoriesId.setWidth(100);
		tblclmnPluginRepositoriesId.setText("ID");

		TableColumn tblclmnPluginRepositoriesName = new TableColumn(tablePluginRepositories, SWT.NONE);
		tblclmnPluginRepositoriesName.setWidth(100);
		tblclmnPluginRepositoriesName.setText("Name");

		TableColumn tblclmnPluginRepositoriesUrl = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesUrl.setMoveable(true);
		tblclmnPluginRepositoriesUrl.setWidth(197);
		tblclmnPluginRepositoriesUrl.setText("URL");

		TableColumn tblclmnPluginRepositoriesLayout = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesLayout.setMoveable(true);
		tblclmnPluginRepositoriesLayout.setWidth(100);
		tblclmnPluginRepositoriesLayout.setText("Layout");

		Button btnPluginRepositoriesDeleteAll = new Button(compositePluginRepositories, SWT.NONE);
		btnPluginRepositoriesDeleteAll.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getPluginRepositories().clear();
				GuiUtils.fillTablePluginRepositoriesItem(tablePluginRepositories, resultProfile.getPluginRepositories());
			}
		});
		btnPluginRepositoriesDeleteAll.setText("Delete All");

		btnPluginRepositoriesDelete.setEnabled(false);
		btnPluginRepositoriesDelete.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				resultProfile.getPluginRepositories().remove(tablePluginRepositories.getSelectionIndex());
			}
		});
		btnPluginRepositoriesDelete.setText("Delete");

		btnPluginRepositoriesEdit.setEnabled(false);
		btnPluginRepositoriesEdit.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				openFrmPluginRepositoryEdit(tablePluginRepositories.getSelectionIndex(), resultProfile);
			}
		});
		btnPluginRepositoriesEdit.setText("Edit");

		Button btnPluginRepositoriesAdd = new Button(compositePluginRepositories, SWT.NONE);
		btnPluginRepositoriesAdd.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				FrmPluginRepository frmPluginRepository = new FrmPluginRepository(shlProfile, resultProfile.getPluginRepositories());
				PluginRepository pluginRepository = frmPluginRepository.open(null);
				if (pluginRepository != null) {
					resultProfile.addPluginRepository(pluginRepository);
					GuiUtils.fillTablePluginRepositoriesItem(tablePluginRepositories, resultProfile.getPluginRepositories());

				}
			}
		});
		btnPluginRepositoriesAdd.setText("Add");
		GroupLayout gl_compositePluginRepositories = new GroupLayout(compositePluginRepositories);
		gl_compositePluginRepositories.setHorizontalGroup(gl_compositePluginRepositories
				.createParallelGroup(GroupLayout.LEADING)
				.add(tablePluginRepositories, GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
				.add(gl_compositePluginRepositories.createSequentialGroup().add(148).add(btnPluginRepositoriesAdd, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnPluginRepositoriesEdit, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6)
						.add(btnPluginRepositoriesDelete, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnPluginRepositoriesDeleteAll, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)));
		gl_compositePluginRepositories.setVerticalGroup(gl_compositePluginRepositories.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositePluginRepositories.createSequentialGroup().add(tablePluginRepositories, GroupLayout.PREFERRED_SIZE, 335, GroupLayout.PREFERRED_SIZE).add(51)
						.add(gl_compositePluginRepositories.createParallelGroup(GroupLayout.LEADING).add(btnPluginRepositoriesAdd).add(btnPluginRepositoriesEdit).add(btnPluginRepositoriesDelete).add(btnPluginRepositoriesDeleteAll))));
		compositePluginRepositories.setLayout(gl_compositePluginRepositories);

		labelWarning = new Label(group, SWT.NONE);
		labelWarning.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Label labelHelpProfile = new Label(group, SWT.NONE);
		labelHelpProfile.setToolTipText("Modifications to the build process which is keyed on some sort of environmental parameter.");
		labelHelpProfile.setImage(ImageFactory.HELP);
		GroupLayout gl_shlProfile = new GroupLayout(shlProfile);
		gl_shlProfile.setHorizontalGroup(gl_shlProfile.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE));
		gl_shlProfile.setVerticalGroup(gl_shlProfile.createParallelGroup(GroupLayout.TRAILING).add(GroupLayout.LEADING, composite, GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE));

		Label labelHelpProfileID = new Label(group, SWT.NONE);
		labelHelpProfileID.setToolTipText("A unique identifier.");
		labelHelpProfileID.setImage(ImageFactory.HELP);
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite
						.createSequentialGroup()
						.add(gl_composite.createParallelGroup(GroupLayout.TRAILING).add(GroupLayout.LEADING, group, 0, 0, Short.MAX_VALUE)
								.add(GroupLayout.LEADING, gl_composite.createSequentialGroup().add(372).add(btnSave, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(6).add(btnCancel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))).add(0)));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(group, GroupLayout.PREFERRED_SIZE, 545, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(gl_composite.createParallelGroup(GroupLayout.LEADING).add(btnSave).add(btnCancel))));
		GroupLayout gl_group = new GroupLayout(group);
		gl_group.setHorizontalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(gl_group.createSequentialGroup().add(536).add(labelHelpProfile))
				.add(gl_group.createSequentialGroup().add(10).add(lblProfileId, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE).add(6).add(textProfileID, GroupLayout.PREFERRED_SIZE, 447, GroupLayout.PREFERRED_SIZE).add(14).add(labelHelpProfileID))
				.add(tabFolderProfileItems, GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE).add(gl_group.createSequentialGroup().add(10).add(labelWarning, GroupLayout.PREFERRED_SIZE, 542, GroupLayout.PREFERRED_SIZE)));
		gl_group.setVerticalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(
				gl_group.createSequentialGroup()
						.add(labelHelpProfile, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
						.add(2)
						.add(gl_group.createParallelGroup(GroupLayout.LEADING).add(gl_group.createSequentialGroup().add(3).add(lblProfileId)).add(textProfileID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.add(gl_group.createSequentialGroup().add(2).add(labelHelpProfileID, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))).add(4).add(tabFolderProfileItems, GroupLayout.PREFERRED_SIZE, 459, GroupLayout.PREFERRED_SIZE).add(6)
						.add(labelWarning)));
		group.setLayout(gl_group);
		composite.setLayout(gl_composite);
		shlProfile.setLayout(gl_shlProfile);

	}

	private final boolean existProfileId(Profile resultProfile) {
		for (Profile existProfileId : listOfProfiles) {
			if (this.existsProfile != null) {
				if (!existsProfile.equals(existProfileId)) {
					if (GuiUtils.formateNull(existProfileId.getId()).trim().equalsIgnoreCase(GuiUtils.formateNull(resultProfile.getId().trim()))) {
						return true;
					}
				}
			} else {
				if (GuiUtils.formateNull(existProfileId.getId()).trim().equalsIgnoreCase(GuiUtils.formateNull(resultProfile.getId().trim()))) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * {@link FrmRepository}
	 */
	private final void openFrmRepositoryEdit(int index, Profile resultProfile) {
		FrmRepository frmRepository = new FrmRepository(shlProfile, resultProfile.getRepositories());
		Repository repository = frmRepository.open(resultProfile.getRepositories().get(index));
		if (repository != null) {
			resultProfile.getRepositories().set(index, repository);
			GuiUtils.fillTableRepositoriesItem(tableRepositories, resultProfile.getRepositories());
		}
	}

	/**
	 * {@link FrmPluginRepository}
	 */
	private final void openFrmPluginRepositoryEdit(int index, Profile resultProfile) {
		FrmPluginRepository frmPluginRepository = new FrmPluginRepository(shlProfile, resultProfile.getPluginRepositories());
		PluginRepository pluginRepository = frmPluginRepository.open(resultProfile.getPluginRepositories().get(index));
		if (pluginRepository != null) {
			resultProfile.getPluginRepositories().set(index, pluginRepository);
			GuiUtils.fillTablePluginRepositoriesItem(tablePluginRepositories, resultProfile.getPluginRepositories());
		}
	}

	/**
	 * {@link FrmProperty}
	 * 
	 * @param name
	 * @param value
	 */
	private final void openFrmPropertyEditor(Profile resultProfile, String name, String value) {
		FrmProperty frmProperty = new FrmProperty(shlProfile);
		String[] properties = frmProperty.open(name, value);
		if (properties != null) {
			resultProfile.getProperties().remove(name);
			resultProfile.getProperties().put(properties[0], properties[1]);
			GuiUtils.fillTablePropertiesItem(tableProperties, resultProfile.getProperties());
		}
	}
}
